package com.test.controller;

import android.content.Context;
import android.os.Bundle;
import android.widget.CalendarView;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.test.R;

import java.util.Calendar;

/**
 * 时间选择器
 *
 * @Author Musique
 * 2020/8/1
 */
public class DatePickerActivity extends AppCompatActivity {

    private Context mContext;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.datepicker_demo);
        mContext = this;


        // DatePicker
        DatePicker datePicker = findViewById(R.id.dp_time);
        Calendar calendar = Calendar.getInstance();
        final int year = calendar.get(Calendar.YEAR);
        final int month = calendar.get(Calendar.DAY_OF_MONTH);
        final int date = calendar.get(Calendar.DATE);
        datePicker.init(year, month, date, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker datePicker, int i, int i1, int i2) {
                Toast.makeText(mContext, year + "年" + (month + 1) + "月" + date + "日",
                        Toast.LENGTH_SHORT).show();
            }
        });

        // TimePicker
        TimePicker timePicker = findViewById(R.id.tp_time);
        timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker timePicker, int hour, int minute) {
                Toast.makeText(mContext, hour + "：" + minute, Toast.LENGTH_SHORT).show();
            }
        });


        //CalendarView
        CalendarView calendarView = (CalendarView) findViewById(R.id.cv_time);
        calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
                Toast.makeText(mContext, year + "年" + month + "月" + dayOfMonth + "日", Toast.LENGTH_SHORT).show();
            }
        });
    }


}
